<template>
  <div class="text-editor">
    <div class="editor-header">
      <button class="back-btn" @click="backRouter">返回</button>
      <button class="save-btn" @click="saveContent">保存</button>
    </div>
    <div class="editor-wrapper">
      <div class="line-numbers">
        <div v-for="(_, index) in lines" :key="index" class="line-number">
          {{ index + 1 }}
        </div>
      </div>
      <textarea
          ref="textareaRef"
          v-model="editorContent"
          class="editor-textarea"
          @input="handleInput"
          @scroll="syncScroll"
      ></textarea>
    </div>
  </div>
</template>

<script lang="ts" setup>
import {ref, computed, onMounted, onBeforeUnmount} from 'vue';
import {uploadTextAsFile} from "@/views/documentManager/components/ts/uploadFile";
import {isAppCollapsed} from "@/main";
import router from "@/router";
import {useRoute} from "vue-router";
import {message} from "@/utils/ElmessasgeUtils";
import {getPureTextContent} from "@/api/documentManage/minioManager2";

const route = useRoute()

const fileFullPath = route.query.fileFullPath as string;
const fileName = route.query.fileName as string;
const filePath = route.query.filePath as string;
const bucket = route.query.bucket as string;

const editorContent = ref('');

onMounted(async () => {
  let response = await getPureTextContent(bucket, fileFullPath);

  if (response.data.code === 200) {
    editorContent.value = response.data.data;

  } else {
    message("获取文件内容失败", 'error')
  }
})

const textareaRef = ref<HTMLTextAreaElement | null>(null);


const lines = computed(() => {
  return editorContent.value.split('\n');
});

// 同步行号和文本区域的滚动
const syncScroll = () => {
  if (textareaRef.value) {
    const lineNumbers = document.querySelector('.line-numbers') as HTMLElement;
    if (lineNumbers) {
      lineNumbers.scrollTop = textareaRef.value.scrollTop;
    }
  }
};

// 处理输入事件，确保行号更新
const handleInput = () => {
  // 这里可以添加额外的输入处理逻辑
};

// 保存内容
const saveContent = async () => {
  await uploadTextAsFile(editorContent.value, filePath, fileName)
};

function backRouter() {
  router.back();
}

onMounted(() => {
  isAppCollapsed.value = true;
  // 初始时确保行号区域高度与文本区域一致
  if (textareaRef.value) {
    const lineNumbers = document.querySelector('.line-numbers') as HTMLElement;
    if (lineNumbers) {
      lineNumbers.style.height = `${textareaRef.value.offsetHeight}px`;
    }
  }
});

onBeforeUnmount(() => {
  isAppCollapsed.value = false;
})
</script>

<style scoped>
.text-editor {
  border: 1px solid #ddd;
  border-radius: 4px;
  overflow: hidden;
  height: 100%;
}

.editor-header {
  background-color: #f5f5f5;
  padding: 8px 16px;
  border-bottom: 1px solid #ddd;
}

.back-btn {
  margin-right: 20px;
  background-color: #3e91f3;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
}

.back-btn:hover {
  background-color: #3e91f3;
}

.save-btn {
  background-color: #4CAF50;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
}

.save-btn:hover {
  background-color: #45a049;
}

.editor-wrapper {
  display: flex;
  position: relative;
  height: 85vh;
}

.line-numbers {
  width: 40px;
  background-color: #f5f5f5;
  padding: 10px 5px;
  overflow-y: hidden;
  text-align: right;
  border-right: 1px solid #ddd;
  color: #666;
  font-family: monospace;
  user-select: none;
}

.line-number {
  line-height: 1.5;
}

.editor-textarea {
  flex: 1;
  padding: 10px;
  border: none;
  resize: none;
  outline: none;
  font-family: monospace;
  line-height: 1.5;
  white-space: pre;
  overflow-y: auto;
}
</style>